﻿using AnlianCommon.API.Commands.RolePerms;
using AnlianCommon.Common.Domains.BeginningManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.Startofseasons
{
    public class StartofseasonDeleteHander : IRequestHandler<StartofseasonDeleteCommand, Result>
    {
        private readonly IBaseRepository<Startofseason> _startofseasonRepository;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="startofseasonRepository"></param>
        /// <param name="mapper"></param>
        public StartofseasonDeleteHander(IBaseRepository<Startofseason> startofseasonRepository, IMapper mapper)
        {
            _startofseasonRepository = startofseasonRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 处理删除命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(StartofseasonDeleteCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Deleted };
            try
            {
                var startofseason = await(await _startofseasonRepository.GetAll()).FirstOrDefaultAsync();
                if (startofseason != null)
                {
                    startofseason.IsDeleted = true;
                    startofseason.DeletedOwnerId = "张三";
                    startofseason.DeletedTime = DateTime.Now;
                    var res = await _startofseasonRepository.Update(startofseason);
                    if (res > 0)
                    {
                        result.Code = (int)CommonErrorCode.Success;
                        result.Msg = "删除成功";
                    }
                    else
                    {
                        result.Code = (int)CommonErrorCode.Deleted;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
 